package com.ywxx.dyhelper.base;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ywxx.dyhelper.R;
import com.ywxx.dyhelper.model.TaskConfig;
import com.ywxx.dyhelper.service.TaskService;
import com.ywxx.dyhelper.utils.ConstValues;

import androidx.appcompat.app.AppCompatActivity;

public abstract class BaseAcitivity extends AppCompatActivity
{

    protected int TaskType = -1;
    protected TextView labLeft;
    protected TextView labRight;
    protected TextView labTitle;
    protected LinearLayout llStart;
    private String title = "";
    protected TaskConfig appconfig = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
    }

    protected void InitTitle()
    {
        labLeft = findViewById(R.id.labLeft);
        labRight = findViewById(R.id.labRight);
        labTitle = findViewById(R.id.labTitle);
        llStart = findViewById(R.id.llStart);

        title = getIntent().getStringExtra("Title");
        TaskType = getIntent().getIntExtra("TaskType", -1);
        if (TextUtils.isEmpty(title) == false)
            labTitle.setText(title);

        labLeft.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
            }
        });
    }


    protected abstract void InitUI();

    protected abstract void DataToUI();

    /**
     * 启动执行任务
     * @param type -1表示首页
     */
    protected void StartTaskServer(int type)
    {
        if (isAccessibilitySettingsOn())
        {
            if (type > -1)
            {
                Intent intent = new Intent(ConstValues.StartTaskAction);
                intent.putExtra("TaskType", type);
                intent.putExtra("TaskConfig", appconfig);
                intent.putExtra("TaskName", title.replace("配置", ""));
                sendBroadcast(intent);

                OpenDouyin();
            }
        } else
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("温馨提示");
            builder.setMessage("【" + getResources().getString(R.string.app_name) + "】" + "无障碍服务未开启");
            builder.setCancelable(false);
            builder.setPositiveButton("立即开启", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    try
                    {
                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        startActivity(intent);
                    } catch (Exception e)
                    {
                        Toast.makeText(BaseAcitivity.this, "打开应用出错", Toast.LENGTH_LONG).show();
                    }
                }
            });
            builder.show();
        }
    }

    protected boolean OpenDouyin()
    {
        boolean b = ConstValues.CheckAppInstalled(this, "com.ss.android.ugc.aweme");
        if (b)
        {
            Intent intent = getPackageManager().getLaunchIntentForPackage("com.ss.android.ugc.aweme");
            if (intent != null)
            {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            return true;
        } else
        {
            Toast.makeText(this, "未安装此应用", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    /**
     * 判断当前服务是否正在运行
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    // To check if service is enabled
    protected boolean isAccessibilitySettingsOn()
    {
        int accessibilityEnabled = 0;
        final String service = getPackageName() + "/" + TaskService.class.getCanonicalName();
        try
        {
            accessibilityEnabled = Settings.Secure.getInt(getApplicationContext().getContentResolver(),
                android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e)
        {
            Log.e("辅助服务", "Error finding setting, default accessibility to not found: " + e.getMessage());
        }
        TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

        if (accessibilityEnabled == 1)
        {
            Log.v("辅助服务", "***ACCESSIBILITY IS ENABLED*** -----------------");
            String settingValue = Settings.Secure.getString(getApplicationContext().getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null)
            {
                mStringColonSplitter.setString(settingValue);
                while (mStringColonSplitter.hasNext())
                {
                    String accessibilityService = mStringColonSplitter.next();

                    Log.v("辅助服务", "-------------- > accessibilityService :: " + accessibilityService + " " + service);
                    if (accessibilityService.equalsIgnoreCase(service))
                    {
                        Log.v("辅助服务", "We've found the correct setting - accessibility is switched on!");
                        return true;
                    }
                }
            }
        } else
        {
            Log.v("辅助服务", "***ACCESSIBILITY IS DISABLED***");
        }

        return false;
    }

}
